<?php

namespace App\Http\Controllers\Admin;

use App\Model\Lesson;
use App\Model\LessonTag;
use App\Model\Tag;
use App\Model\Video;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取所有课程数据
        $lessons = Lesson::paginate(10);
        //加载课程列表模板
        return view('admin.lesson.index',compact('lessons'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取当前标签表中的所有标签数据
        $tags = Tag::get();
        //加载添加课程模板
        return view('admin.lesson.create',compact('tags'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //将post数据存入lesson表中
        $lesson = Lesson::create($request->all());
        //将选择的标签存入lessonTag表中
        //循环post数据中的tags数组,循环将每一个标签数据存入lessontag表
        foreach ($request->input('tags') as $v) {
            $lesson->lessonTag()->create([
                'tag_id' => $v,
            ]);
        }
        //将视频数据存入videos表
        //将post数据中的videos转成php数组,然后循环post数据中的videos,
        $videos = json_decode($request->input('videos'),true);
        foreach ($videos as $v) {
            $lesson->videos()->create($v);
        }
        session()->flash('success','课程添加成功');
        return redirect()->route('lesson.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function show(Lesson $lesson)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function edit(Lesson $lesson)
    {
        //获取所有标签数据并分配
        $tags = Tag::get();
        //获取当前课程的标签数据
        $lessonTags = LessonTag::where('lesson_id',$lesson['id'])->pluck('tag_id')->toArray();
        //获取当前课程的所有视频数据
        //由于前台使用的视频数据是js的数组,我们应该将获取到的视频数据转成json数据,然后再分配
        $videos = json_encode($lesson->videos->toArray());
        //加载编辑课程模板
        return view('admin.lesson.edit',compact('tags','lesson','lessonTags','videos'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Lesson $lesson)
    {
        //先将课程表的数据改成新的数据
        $lesson->title = $request->input('title');
        $lesson->introduce = $request->input('introduce');
        $lesson->thumb = $request->input('thumb');
        $lesson->is_commend = $request->input('is_commend');
        $lesson->is_hot = $request->input('is_hot');
        $lesson->click = $request->input('click');
        $lesson->save();
        //修改当前课程的标签数据
        //首先将当前课程的标签数据删除,然后再循环添加
        LessonTag::where('lesson_id',$lesson['id'])->delete();
        foreach ($request->input('tags') as $v) {
            $lesson->lessonTag()->create([
                'tag_id' => $v,
            ]);
        }
        //修改当前课程的视频数据
        //先将当前课程的视频数据删除,然后将新的视频数据添加
        Video::where('lesson_id',$lesson['id'])->delete();
        $videos = json_decode($request->input('videos'),true);
        foreach ($videos as $v) {
            $lesson->videos()->create($v);
        }
        //设置闪存信息并返回
        session()->flash('success','课程修改成功');
        return redirect()->route('lesson.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Lesson  $lesson
     * @return \Illuminate\Http\Response
     */
    public function destroy(Lesson $lesson)
    {
        //删除对应课程的标签数据
        LessonTag::where('lesson_id',$lesson['id'])->delete();
        //删除对应课程的视频数据
        Video::where('lesson_id',$lesson['id'])->delete();
        //删除对应课程数据
        $lesson->delete();
        //设置闪存并返回
        session()->flash('success','课程数据删除成功');
        return redirect()->route('lesson.index');
    }
}
